package com.threesome.memos.dialog;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;

import com.threesome.memos.R;
import com.threesome.memos.wheel.NumericWheelAdapter;
import com.threesome.memos.wheel.OnWheelChangedListener;
import com.threesome.memos.wheel.WheelView;

public class DatePicker extends Activity implements OnWheelChangedListener, OnClickListener {
	public static final String YEAR = "year";
	public static final String MONTH = "month";
	public static final String DAY = "day";
	private static final List<Integer> MONTHS_BIG = 
			Arrays.asList(new Integer[]{1, 3, 5, 7, 8, 10, 12});
	private static final List<Integer> MONTHS_LITTLE = 
			Arrays.asList(new Integer[]{4, 6, 9, 11});
	private static final int MAX_YEARS_NUM = 5;
	private WheelView mYearWheel;
	private WheelView mMonthWheel;
	private WheelView mDayWheel;
	private int mStartYear;
	private int mYear;
	private int mMonth;
	private int mDay;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_date_picker);
		
		mYearWheel = (WheelView)findViewById(R.id.year_wheel);
		mMonthWheel = (WheelView)findViewById(R.id.month_wheel);
		mDayWheel = (WheelView)findViewById(R.id.day_wheel);
		
		Calendar calendar = Calendar.getInstance();
		mStartYear = calendar.get(Calendar.YEAR);
		mYear = getIntent().getIntExtra(YEAR, calendar.get(Calendar.YEAR));
		mMonth = getIntent().getIntExtra(MONTH, calendar.get(Calendar.MONTH)+1);
		mDay = getIntent().getIntExtra(DAY, calendar.get(Calendar.DATE));
		
		mYearWheel.setAdapter(new NumericWheelAdapter(mStartYear, mStartYear + MAX_YEARS_NUM, "00"));
		mYearWheel.setCyclic(true);
		mYearWheel.setCurrentItem(mYear - mStartYear);
		mYearWheel.setLabel(getString(R.string.year));
		mYearWheel.TEXT_SIZE = 18;
		
		mMonthWheel.setAdapter(new NumericWheelAdapter(1, 12, "00"));
		mMonthWheel.setCyclic(true);
		mMonthWheel.setCurrentItem(mMonth-1);
		mMonthWheel.setLabel(getString(R.string.month));
		mMonthWheel.TEXT_SIZE = 18;
		
		setDayWheelAdapter();
		mDayWheel.setCyclic(true);
		mDayWheel.setLabel(getString(R.string.day));
		mDayWheel.TEXT_SIZE = 18;
		
		mYearWheel.addChangingListener(this);
		mMonthWheel.addChangingListener(this);
		mDayWheel.addChangingListener(this);
		findViewById(R.id.save).setOnClickListener(this);
		findViewById(R.id.cancel).setOnClickListener(this);
	}
	
	private void setDayWheelAdapter() {		
		int endDay;
		if(MONTHS_BIG.contains(mMonth)) {
			endDay = 31;
		} 
		else if(MONTHS_LITTLE.contains(mMonth)) {
			endDay = 30;
		} 
		else {
			if ((mYear % 4 == 0 && mYear % 100 != 0) || mYear % 400 == 0) {
				endDay = 29;
			}	
			else {
				endDay = 28;
			}
		}
		mDayWheel.setAdapter(new NumericWheelAdapter(1, endDay, "00"));
		if(mDay >= endDay) {
			mDayWheel.setCurrentItem(endDay-1);
		}
		else {
			mDayWheel.setCurrentItem(mDay-1);
		}
	}


	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		switch (wheel.getId()) {
		case R.id.year_wheel:
			mYear = newValue + mStartYear;
			mMonth = mMonthWheel.getCurrentItem() + 1;
			mDay = mDayWheel.getCurrentItem() + 1;
			break;
		case R.id.month_wheel:
			mYear = mYearWheel.getCurrentItem() + mStartYear;
			mMonth = newValue + 1;
			mDay = mDayWheel.getCurrentItem() + 1;
			break;
		case R.id.day_wheel:
			mDay = newValue + 1;
			break;
		default:
			break;
		}
		setDayWheelAdapter();
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancel:
			finish();
			break;
		case R.id.save:
			Intent intent = new Intent();
			intent.putExtra(YEAR, mYear);
			intent.putExtra(MONTH, mMonth);
			intent.putExtra(DAY, mDay);
			setResult(Activity.RESULT_OK, intent);
			finish();
			break;
		default:
			break;
		}
	}
}
